import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
import logging

from models.attendce import Attendce
from models.user import User

scheduler = BlockingScheduler()   # 后台运行
attenduce = Attendce()
 # 设置为每日凌晨23:30:30时执行一次调度程序
#@scheduler.scheduled_job("cron", day_of_week='*', hour='23', minute='30', second='30')
def rebate():
        print("schedule execute")
        users = attenduce.attendce_later_user()
        for user in users:
            attenduce.attendce_create(user_id=user.get("id"), attendce_date=datetime.date.today(),
                                      attendce_time=datetime.datetime.now(), not_attendce=True)
        logging.debug("statistic scheduler execute success" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))


if __name__ == '__main__':
    try:
        #scheduler.start()
        rebate()
        logging.debug("statistic scheduler start success")
    except (KeyboardInterrupt, SystemExit):
        scheduler.shutdown()
        logging.debug("statistic scheduler start-up fail")